package reader;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.bind.DatatypeConverter;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.shhic.itsed.mws.deviceintf.AntennaProtocol;
import com.shhic.itsed.mws.deviceintf.RawTagBank;
import com.shhic.itsed.mws.deviceintf.RawTagField;
import com.shhic.itsed.mws.deviceintf.ReaderCommand;
import com.shhic.itsed.mws.deviceintf.ReaderCommandType;
import com.shhic.itsed.mws.deviceintf.ReaderLinkStatusEnum;
import com.tjkcht.midware.reader.Rd30Manager;

import rdp_msg.Rdp.rdp_request;
import rdp_msg.Rdp.rdp_request.HeadType;

public class ReaderManagerTest2 {
	private String ip1 = "192.168.11.180";
	private String ip2 = "192.168.11.181";
	
	private int port = 5084;
	private Rd30Manager rm;
	private ReaderCommand cmd1;
	private ReaderCommand cmd2;
	public ResultCallBack call1=new ResultCallBack();
	public ResultCallBack call2=new ResultCallBack();

	@Before
	public void setUp() throws Exception {
		rm = new Rd30Manager();
		cmd1=getCmd(ip1,1);
		cmd2=getCmd(ip2,2);
	}
	private ReaderCommand getCmd(String ip,int ann){
		ReaderCommand cmd = new ReaderCommand();
		cmd.setReaderIp(ip);
		cmd.setReaderPort(port);
		cmd.setReaderCommandType(ReaderCommandType.CYCLE);
		Set<AntennaProtocol> antennaProtocolSet=new HashSet<AntennaProtocol>();
		AntennaProtocol ap=new AntennaProtocol();
		ap.setAntennaNo(ann);
		antennaProtocolSet.add(ap);
		Set<String> plSet=new HashSet<String>();
		plSet.add("EPC");
		ap.setProtocols(plSet);			
		cmd.setAntennaProtocolSet(antennaProtocolSet);
		List<RawTagField> readTagFieldList=new ArrayList<RawTagField>();
		RawTagField field=new RawTagField();
		field.setBank(RawTagBank.EPCBANK);
		field.setOffset(0);
		field.setLength(2);			
		readTagFieldList.add(field);
		cmd.setReadTagFieldList(readTagFieldList);
		return cmd;
	}

	@After
	public void tearDown() throws Exception {
		rm.close();
	}

	@Test
	public void test() {
		rm.initReader(ip1, port);
		rm.initReader(ip2, port);
		try {
//			assertEquals(ReaderLinkStatusEnum.CONNECTING, rm.queryDeviceLinkstatus(ip1));
//			Thread.sleep(1000);	
//			assertEquals(ReaderLinkStatusEnum.CONNECTING, rm.queryDeviceLinkstatus(ip2));
			///send reader config
//			Thread.sleep(1000);
		
//			Thread.sleep(1000);
			rm.dispatchReadCommand(ip2, cmd2, 0, call2);			
			rm.dispatchReadCommand(ip1, cmd1, 0, call1);
			Thread.sleep(50000);			
			rm.stopReaderCommand(ip1);
//			Thread.sleep(1000);
			rm.stopReaderCommand(ip2);
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
